const Koa = require('koa')
const app = new Koa()
const views = require('koa-views')
const json = require('koa-json')
const onerror = require('koa-onerror')
const bodyparser = require('koa-bodyparser')
const logger = require('koa-logger')

const index = require('./routes/index')
const users = require('./routes/users')

// error handler
onerror(app)

// middlewares
app.use(bodyparser({
  enableTypes: ['json', 'form', 'text']
}))
app.use(json())
app.use(logger())
app.use(require('koa-static')(__dirname + '/public'))

app.use(views(__dirname + '/views', {
  extension: 'pug'
}))
 
global.users = [
  //学生
  {
    id: 1,
    name: '张三',
    username: '123456',
    password: '123456',
    class: '1班',
    educatId: '8888',
    phone: '15832086256',
    grade: '2019',
    school: '振涛',
    EditeScore: '1',
    role: ['student']
  },
  {
    id: 2,
    name: '李四',
    username: '090909',
    password: '090909',
    class: '1班',
    educatId: '9999',
    phone: '16768141569',
    grade: '2019',
    school: '振涛',
    EditeScore: '0'
  },
  //教师1
  {
    id: 1001,
    nickname: '小王老师',
    username: '09876',
    password: '098765',
    name: '王鼎',
    subject: '数学',
    school: '振涛',
    ManageClass: [
      { id: 1, name: '1班' },
    ],
    EditeScore: '0',
    SheetScore: '0',
    role: ['teacher']
  },
  {
    id: 1002,
    nickname: '小张老师',
    username: '098765',
    password: '098765',
    name: '张三',
    subject: '语文',
    school: '振涛',
    ManageClass: [
      { id: 2, name: '2班' },
    ],
    EditeScore: '0',
    SheetScore: '0'
  },
  //管理员 S
  {
    id: 2001,
    username: 'admin',
    password: '123456', 
    name: '管理员',
    school: '振涛',
    SheetScore: '0',
    role: ['teacher', 'admin']
  }
]

global.usersScore = {
  banjidata: [
    {
      id: 1,
      name: "小孔子",
      Account: 20,
      works: 70,
      editor: 40,
      like: 50,
      review: 60,
    },
    {
      id: 2,
      name: "小孩子",
      Account: 20,
      works: 70,
      editor: 40,
      like: 50,
      review: 60,
    },
    {
      id: 3,
      name: "小凯子",
      Account: 20,
      works: 70,
      editor: 40,
      like: 50,
      review: 60,
    }
  ],
  guanbanji: [
    {
      grade: "海量一班",
      teacher: "小孔子",
      id: 1
    },
    {
      grade: "海量二班",
      teacher: "小凯子",
      id: 2
    },
    {
      grade: "海量三班",
      teacher: "小耗子",
      id: 3
    }
  ],
  guanjiaoshi: [
    {
      id: 1,
      mingzi: "小孔子老师",
      zhanghaos: "123456",
      namess: "孔子",
      phone: "17565225984",
    },
    {
      id: 2,
      mingzi: "小耗子老师",
      zhanghaos: "123456",
      namess: "耗子",
      phone: "17565225984",
    },
    {
      id: 3,
      mingzi: "小凯子老师",
      zhanghaos: "123456",
      namess: "凯子",
      phone: "17565225984",
    }
  ]
}


//编辑区
global.list = [

]

//个人公开作品
global.list2 = [ 

]


//社区
global.list3 = [

]

//班级数据
global.list4=[
  
]

//分类
global.listType ={
  //社区
  CommData:[
    {id:1,value:'全部',label:'全部'},
    {id:2,value:'热门',label:'热门'},
  ]
} 


// logger
app.use(async (ctx, next) => {
  const start = new Date()
  await next()
  const ms = new Date() - start
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
})

// routes
app.use(index.routes(), index.allowedMethods())
app.use(users.routes(), users.allowedMethods())

// error-handling
app.on('error', (err, ctx) => {
  console.error('server error', err, ctx)
});

module.exports = app
